package com.wyl.template.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyl.template.aop.annotation.SysLogger;
import com.wyl.template.domain.common.PageResult;
import com.wyl.template.domain.common.ResponseResult;
import com.wyl.template.domain.excel.PatientExcel;
import com.wyl.template.domain.model.TPatientDO;
import com.wyl.template.domain.request.PatientPageRequest;
import com.wyl.template.enums.LoggerType;
import com.wyl.template.service.TPatientService;
import com.wyl.template.util.Des3New;
import com.wyl.template.util.DesensitizationUtil;
import com.wyl.template.util.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: wyl
 * @Date: 2021/7/5
 * @Description: 患者管理模块
 */
@SysLogger(group = "患者")
@RestController
@RequestMapping("/patient")
public class PatientController extends BaseController<TPatientService, TPatientDO> {

    /**
     * 查询分页信息
     *
     * @param pageRequest
     * @return
     */
    @SysLogger(name = "分页查询", type = LoggerType.SELECT)
    @GetMapping("patient")
    public ResponseResult listPatient(PatientPageRequest pageRequest) {
        ResponseResult<IPage<TPatientDO>> selectPage = this.selectPage(pageRequest);
        IPage pageInfo = selectPage.getData();
        List<TPatientDO> patientList = pageInfo.getRecords();

        patientList.forEach(patient->{
            try {
                //解密
                String idNumberTemp = Des3New.decodeCBC(patient.getIdNumber());
                //脱敏
                patient.setIdNumber(DesensitizationUtil.commonDisplay(idNumberTemp));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        PageResult pageResult = new PageResult();
        BeanUtils.copyProperties(pageInfo, pageResult);
        pageResult.setRecords(patientList);
        return ResponseResult.success(pageResult);
    }

    /**
     * 导出患者信息
     *
     * @param pageRequest
     * @param response
     */
    @SysLogger(name = "导出患者信息", type = LoggerType.EXPORT)
    @GetMapping("/download")
    public void download(PatientPageRequest pageRequest, HttpServletResponse response) {
        ResponseResult<IPage<TPatientDO>> selectPage = this.selectPage(pageRequest);
        IPage pageInfo = selectPage.getData();
        List<TPatientDO> patientList = pageInfo.getRecords();

        patientList.forEach(patient->{
            try {
                //解密
                String idNumber = Des3New.decodeCBC(patient.getIdNumber());
                patient.setIdNumber(idNumber);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        List<PatientExcel> patientExcels = patientList.stream().map(patient -> {
            PatientExcel patientExcel = new PatientExcel();
            BeanUtils.copyProperties(patient, patientExcel);
            patientExcel.setSex(patient.getSex() == 1 ? "男" : "女");
            return patientExcel;
        }).collect(Collectors.toList());


        ExcelUtil.downloadExcel(response, "患者列表", patientExcels, PatientExcel.class);
    }

}
